package com.huilian.iotbox.admin.service.impl;

import com.huilian.iotbox.admin.dao.StatsPackageStoreDao;
import com.huilian.iotbox.admin.service.StatsPackageStoreService;
import com.huilian.iotbox.data.dto.StatsPackageStoreDto;
import com.huilian.iotbox.data.enums.StatsPackageStorePackageTypeEnum;
import com.huilian.iotbox.data.service.ErrorLogCommonService;
import com.huilian.iotbox.data.vo.StatsPackageStoreVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author LinYinDong
 * @date 2023/7/28 9:47
 */
@Slf4j
@Service
public class StatsPackageStoreServiceImpl implements StatsPackageStoreService {
    @Resource
    private StatsPackageStoreDao statsDayPackageStoreDao;

    @Autowired
    private ErrorLogCommonService errorLogCommonService;
    @Override
    public Exception statsDayPackageStoreJob(String startTime, String endTime, String yesterday,Integer type) {

        Exception temp = null;
        try {
            //根据场地统计门店套餐
            List<StatsPackageStoreVo> statsPackageStoreVo = statsDayPackageStoreDao.statsDayOrderStoreList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageStoreVo)){
                statsPackageStoreVo.forEach(item ->{
                    StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
                    statsPackageStoreDto.setMchStoreId(item.getMchStoreId());
                    statsPackageStoreDto.setType(type);
                    statsPackageStoreDto.setDateStr(yesterday);
                    statsPackageStoreDto.setPackageType(StatsPackageStorePackageTypeEnum.STORE_PACKAGE.getCode());
                    statsPackageStoreDto.setTitle(item.getTitle());
                    statsPackageStoreDto.setTotalAmount(item.getTotalAmount());
                    statsPackageStoreDto.setPayCount(item.getPayCount());
                    statsDayPackageStoreDao.insert(statsPackageStoreDto);
                });
            }

            //根据场地统计抖音套餐
            List<StatsPackageStoreVo> statsPackageDouYinList = statsDayPackageStoreDao.statsPackageDouYinList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageDouYinList)){
                statsPackageDouYinList.forEach(item ->{
                    StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
                    statsPackageStoreDto.setMchStoreId(item.getMchStoreId());
                    statsPackageStoreDto.setType(type);
                    statsPackageStoreDto.setDateStr(yesterday);
                    statsPackageStoreDto.setPackageType(StatsPackageStorePackageTypeEnum.DOUYIN_PACKAGE.getCode());
                    statsPackageStoreDto.setTitle(item.getTitle());
                    statsPackageStoreDto.setTotalAmount(item.getTotalAmount());
                    statsPackageStoreDto.setPayCount(item.getPayCount());
                    statsDayPackageStoreDao.insert(statsPackageStoreDto);
                });
            }

            //根据场地统计美团套餐
            List<StatsPackageStoreVo> statsPackageMeiTuanList = statsDayPackageStoreDao.statsPackageMeiTuanList(startTime, endTime);
            if(!ObjectUtils.isEmpty(statsPackageMeiTuanList)){
                statsPackageMeiTuanList.forEach(item ->{
                    StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
                    statsPackageStoreDto.setMchStoreId(item.getMchStoreId());
                    statsPackageStoreDto.setType(type);
                    statsPackageStoreDto.setDateStr(yesterday);
                    statsPackageStoreDto.setPackageType(StatsPackageStorePackageTypeEnum.MEITUAN_PACKAGE.getCode());
                    statsPackageStoreDto.setTitle(item.getTitle());
                    statsPackageStoreDto.setTotalAmount(item.getTotalAmount());
                    statsPackageStoreDto.setPayCount(item.getPayCount());
                    statsDayPackageStoreDao.insert(statsPackageStoreDto);
                });
            }
        }catch (Exception e) {
            e.printStackTrace();
            temp = e;
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();

            StatsPackageStoreDto statsPackageStoreDto = new StatsPackageStoreDto();
            statsPackageStoreDto.setStartTime(startTime);
            statsPackageStoreDto.setEndTime(endTime);
            errorLogCommonService.writerInfoException("套餐订单日统计失败", statsPackageStoreDto, e);
        }
        return temp;
    }

    @Override
    public Exception statsMonthPackageStoreJob(String startTime, String endTime, String previousMonth,Integer type) {
        return this.statsDayPackageStoreJob(startTime,endTime,previousMonth,type);
    }
}
